using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LiteTools.domains.db;

namespace LiteTools.domains
{
    /// <summary>
    /// Tlv数据定义
    /// </summary>
    public class TlvData : FormatData
    {
        /// <summary>
        /// 当前的TLV数据为第几级
        /// </summary>
        public int Level { get; set; }

        /// <summary>
        ///Tlv的Tag
        /// </summary>
        public string Tag { get; set; }

        /// <summary>
        /// Tlv的数据的长度，保存的是长度的数据值，未经转换的完整的值
        /// </summary>
        public string Length { get; set; }

        /// <summary>
        /// Tlv的数据，如果已经是最后一级，就将其填充字符串
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// Tlv数据的集合
        /// </summary>
        public List<TlvData> TlvList { get; set; }

        private Dictionary<string, TagInfoHelper.TagInfo> tagInfoDict;

        public override string Convert2Str(bool addTips, int familyId)
        {
            if (addTips)
            {
                tagInfoDict = new Dictionary<string, TagInfoHelper.TagInfo>();
                var tagInfoByFamily = TagInfoHelper.GetTagInfoByFamily(familyId.ToString());
                if (tagInfoByFamily != null)
                {
                    foreach (var tagInfo in tagInfoByFamily.Where(
                                 tagInfo => !tagInfoDict.ContainsKey(tagInfo.tag_value)))
                    {
                        tagInfoDict.Add(tagInfo.tag_value, tagInfo);
                    }
                }
            }

            var convert2Str = GetTlvString(TlvList, addTips);
            return convert2Str;
        }

        private string GetTlvString(IReadOnlyCollection<TlvData> tlvList, bool addTips)
        {
            var builder = new StringBuilder();
            if (tlvList == null || tlvList.Count == 0)
            {
                return "";
            }

            foreach (var tlvData in tlvList)
            {
                var header = GetHeader(tlvData);
                // 打印Header以及Tag和Length
                builder.Append(header).Append(tlvData.Tag).Append("\t").Append(tlvData.Length);

                // 此时是基础结构，应该在Data之后拼接说明信息
                if (tlvData.TlvList == null || tlvData.TlvList.Count == 0)
                {
                    builder.Append("\t").Append(tlvData.Value);
                    if (addTips)
                    {
                        builder.Append("\t");
                        AddTips(tlvData, builder);
                    }

                    builder.Append("\n");
                }
                // 复杂结构的在Length之后拼接说明信息
                else
                {
                    // 拼装完Length之后在其之后拼装获取到的注释信息
                    if (addTips)
                    {
                        AddTips(tlvData, builder);
                    }

                    var tlvString = GetTlvString(tlvData.TlvList, addTips);
                    builder.Append("\n").Append(tlvString);
                }
            }

            return builder.ToString();
        }

        private void AddTips(TlvData tlvData, StringBuilder builder)
        {
            if (tagInfoDict != null && tagInfoDict.TryGetValue(tlvData.Tag, out var value))
            {
                builder.Append("\t## 【tips: ").Append(value.tag_desc).Append(" Len: ")
                    .Append(Convert.ToInt32(tlvData.Length, 16)).Append("】");
            }
        }

        private static string GetHeader(TlvData tlvData)
        {
            var builder = new StringBuilder();
            for (var i = 0; i < tlvData.Level - 1; i++)
            {
                builder.Append("\t");
            }

            return builder.ToString();
        }
    }
}